<?php
/**
 * Author: zhao-baolin
 * E-mail: qiutian@fengyumeng.com
 */


//微信curl通信
if(!function_exists('curl_post')){
    function curl_post($url, $data,$header = '')
    {
        error_log("\r\n".date('Y-m-d H:i:s').'【'.__LINE__.'】 : '.print_r($data,true),3,'1.log');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS ,$data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($ch);
        error_log("\r\n".date('Y-m-d H:i:s').'【'.__LINE__.'】 : '.print_r($output,true),3,'1.log');
        curl_close($ch);
        return $output;
    }
}


/**
 * 生成四位随机数
 * @return int
 */
if(!function_exists('random')) {
    function random()
    {
        return strval(rand(1000, 9999));
    }
}

/**
 * 获取客户端IP
 * PS: 无法透视VNP 待优化
 * @param bool|false $type 不传默认false 只取IP 传TRUE顺带取地区
 * @return array|string
 */
if(!function_exists('get_ip')) {
    function get_ip($type = false)
    {
        $realip = '';
        if (isset($_SERVER)) {
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }

        $realip = $realip == '::1' ? '127.0.0.1' : $realip;
        $arr = $type == false ? $realip : array('ip' => $realip, 'area' => $realip == '127.0.0.1' ? '本地' : ipToArea($realip));
        return $arr;
    }
}



if(!function_exists('get_order_id')) {
    //获取唯一订单号
    function get_order_id()
    {
        return date('YmdHis').random_int(10,99);
    }
}



/**
 * 数据XML编码
 * @param mixed  $data 数据
 * @param string $item 数字索引时的节点名称
 * @param string $id   数字索引key转换为的属性名
 * @return string
 */
function arr_to_xml($data, $item='item', $id='id') {
    $xml  = '<xml>';
    foreach ($data as $key => $val) {
        if(is_numeric($key)){
            $id && $attr = " {$id}=\"{$key}\"";
            $key  = $item;
        }
        $xml    .=  "<{$key}>";
        $xml    .=  (is_array($val) || is_object($val)) ? data_to_xml($val, $item, $id) : $val;
        $xml    .=  "</{$key}>";
    }
    $xml .= '</xml>';
    return $xml;
}

function xm_to_arr($xml){

    //禁止引用外部xml实体

    libxml_disable_entity_loader(true);

    $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

    $val = json_decode(json_encode($xmlstring),true);

    return $val;

}

/**
 * XML编码
 * @param mixed $data 数据
 * @param string $root 根节点名
 * @param string $item 数字索引的子节点名
 * @param string $attr 根节点属性
 * @param string $id   数字索引子节点key转换的属性名
 * @param string $encoding 数据编码
 * @return string
 */
function xml_encode($data, $root='think', $item='item', $attr='', $id='id', $encoding='utf-8') {
    if(is_array($attr)){
        $_attr = array();
        foreach ($attr as $key => $value) {
            $_attr[] = "{$key}=\"{$value}\"";
        }
        $attr = implode(' ', $_attr);
    }
    $attr   = trim($attr);
    $attr   = empty($attr) ? '' : " {$attr}";
    $xml    = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
    $xml   .= "<{$root}{$attr}>";
    $xml   .= data_to_xml($data, $item, $id);
    $xml   .= "</{$root}>";
    return $xml;
}
